$(function () {

    var args = {
        pageId: null,
        pPageId: null,
        $groupBtns: null,
        queryScope: 'canIn'
    };

    var interfaces = {
        getEmpListUrl: $.contextPath + "emp/getEmpList",
        getDepCommbox: $.contextPath + "dep/getDepCommbox",
        getEmpById: $.contextPath + "emp/getEmpById",
        delEmpById: $.contextPath + "emp/delEmpById",
    };

    var jsonPosts = {
        getEmpById: function (id, onAfter) {
            jqueryUtil.jsonPost(interfaces.getEmpById, {id: id}, function (rsp) {
                if (onAfter) {
                    onAfter(rsp);
                }
            });
        },
        delEmpById: function (id, onAfter) {
            jqueryUtil.jsonPost(interfaces.delEmpById, {id: id}, function (rsp) {
                if (onAfter) {
                    onAfter(rsp);
                }
            });
        },
    };

    var events = {
        executeQuery: function () {
            executeQuery()
        },
        btnSearch: function () {
            executeQuery()
        }
    }

    var selectors = {
        grid: $("#grid1"),
        form: $("#searchForm"),
        editForm: $("#searchForm"),
        depId: $("#depId"),
        btnSearch: $("#btnSearch"),
        depDlgId: $("#depDlgId"),
    }

    function init() {
        initEvents();
        initCommbox();
        initGrid();
    }

    function initCommbox(rsp) {
        selectors.depId.combobox({
            url: interfaces.getDepCommbox,
            valueField: 'id',
            textField: 'text',
            onLoadSuccess: function () {

            }
        })
    }

    function loadEmp(rsp){
        $("#depDlgId").combobox({
            url: interfaces.getDepCommbox,
            valueField: 'id',
            textField: 'text',
            onLoadSuccess: function () {
                var birthday = getYears(rsp.birthday);
                $("#birthday").datebox('setValue', birthday);
                $("#editForm").form("load", rsp);
            }
        })
    }

    function initEvents() {
        selectors.btnSearch.click(events.btnSearch)
    }

    function initGrid() {
        var self = this;
        selectors.grid.datagrid({
            width: 'auto',
            height: window.innerHeight * 0.9 - 100,
            columns: [[
                {field: 'empId', title: '编号', width: 100},
                {field: 'username', title: '登陆名', width: 100},
                {field: 'name', title: '真实姓名', width: 100},
                {
                    field: 'gender', title: '性别', width: 100, formatter: function (value, row, index) {
                        value = row.gender;
                        if (1 == value * 1) {
                            return '男';
                        }
                        if (0 == value * 1) {
                            return '女';
                        }
                    }
                },
                {field: 'email', title: '邮件地址', width: 150},
                {field: 'tele', title: '联系电话', width: 150},
                {field: 'address', title: '联系地址', width: 150},
                {
                    field: 'birthday', title: '出生年月日', width: 150, formatter: function (value) {
                        if(value){
                            getYears(value);
                        }else {
                            return ""
                        }
                    }
                },
                {
                    field: 'depId', title: '部门编号', width: 100, formatter: function (value) {
                        return value;
                    }
                },

                {
                    field: '-', title: '操作', width: 200, formatter: function (value, row, index) {
                        var oper = "<a href=\"javascript:void(0)\" onclick=\"distributionPermissions(" + row.empId + ')">分配权限</a> ';
                        oper +=" <a href=\"javascript:void(0)\" onclick=\"editDatagrid(" + row.empId + ')">修改</a>';
                        oper += ' <a href="javascript:void(0)" onclick="delEmpById(' + row.empId + ')">删除</a>';
                        return oper;
                    }
                }
            ]],
            singleSelect: true,
            autoRowHeight: true,
            striped: true,
            pagination: true,
            loadMsg: "数据加载中....",
        });

        events.executeQuery();
    }

    function getYears(value) {
        var time = new Date(value);
        var y = time.getFullYear();
        var m = time.getMonth() + 1;
        var d = time.getDate();
        var h = time.getHours();
        var mm = time.getMinutes();
        var s = time.getSeconds();
        return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
    }

    function distributionPermissions() {
        args.$dlg = $('<div />').dialog({
            title: "查看权限",
            href: "emp/dlg/empDlg.jsp",
            width: 600,
            height: 500,
            buttons: [
                {
                    id: "aSave",
                    text: "保存",
                    handler: function () {

                    }
                },
                {
                    id: "aCancel",
                    text: "关闭",
                    handler: function () {
                        args.$dlg.dialog('destroy');
                        args.$dlg = null;
                    }
                }
            ],
            onClose: function () {
                args.$dlg.dialog('destroy');
                args.$dlg = null;
            },
            onLoad: function () {
            }
        });
    }

    function createDlg(id) {
        args.$dlg = $('<div />').dialog({
            title: id != null ? "员工修改" : "新增员工",
            href: "emp/dlg/empDlg.jsp",
            width: 600,
            height: 500,
            buttons: [
                {
                    id: "aSave",
                    text: "保存",
                    handler: function () {
                        var param = $("#form").serializeJSON()
                        jsonPosts.persistenceCategory(param, function (rsp) {
                            if (rsp.success) {
                                $.showMessage(rsp.message)
                                events.executeQuery();
                                args.$dlg.dialog('destroy');
                                args.$dlg = null;
                            } else {
                                $.showMessage(rsp.message)
                            }
                        });
                    }
                },
                {
                    id: "aCancel",
                    text: "关闭",
                    handler: function () {
                        events.executeQuery();
                        args.$dlg.dialog('destroy');
                        args.$dlg = null;
                    }
                }
            ],
            onClose: function () {
                events.executeQuery();
                args.$dlg.dialog('destroy');
                args.$dlg = null;
            },
            onLoad: function () {
                if (id != null) {
                    jsonPosts.getEmpById(id, function (rsp) {
                        loadEmp(rsp)
                    })
                } else {
                    initCommbox();
                }
            }
        });
    }

    this.editDatagrid = function (id) {
        createDlg(id)
    }

    this.delEmpById = function (id) {
        $.messager.confirm('提示信息', '确认删除该员工吗?', function (r) {
            if (r) {
                jsonPosts.delEmpById(id, function (rsp) {
                    if(rsp.success){
                        $.showMessage(rsp.message)
                        events.executeQuery();
                        args.$dlg.dialog('destroy');
                        args.$dlg = null;
                    }else {
                        $.showMessage(rsp.message)
                    }
                })
            }
        });
    }

    function add0(m){return m<10?'0'+m:m }

    function executeQuery() {
        var formData = selectors.form.serializeJSON();
        selectors.grid.datagrid({url: interfaces.getEmpListUrl, queryParams: formData})
    }
    init();
});